const StrUitl = {
  trim(str: string, charsToTrim: string) {
    while (str.startsWith(charsToTrim)) {
      str = this.trimStart(str, charsToTrim);
    }
    while (str.endsWith(charsToTrim)) {
      str = this.trimEnd(str, charsToTrim);
    }
    return str;
  },
  trimStart: function (str: string, charsToTrim: string = ''): string {
    if (!str) return "";
    // 如果没有指定要移除的字符，默认移除空格
    const defaultChars = charsToTrim || ' \t\n\r';
    const regex = new RegExp(`^[${defaultChars}]`);
    return str.replace(regex, '');
  },

  trimEnd: function (str: string, charsToTrim: string = ''): string {
    if (!str) return "";
    // 如果没有指定要移除的字符，默认移除空格
    const defaultChars = charsToTrim || ' \t\n\r';
    const regex = new RegExp(`[${defaultChars}]$`);
    return str.replace(regex, '');
  },
  split: (str: string, seperator: string, removeEmpties: boolean, removeDuplicate: boolean): string[] => {
    if (!str) return [];
    const strList = str.split(seperator);
    const result = [] as string[];
    strList.forEach(x => {
      if (removeEmpties && !x) {
        return;
      }
      if (removeDuplicate && result.includes(x)) {
        return;
      }
      result.push(x);
    })
    return result;
  },
  random(length: number) {
    const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    let result = '';
    for (let i = 0; i < length; i++) {
      result += chars.charAt(Math.floor(Math.random() * chars.length));
    }
    return result;
  }
}

export default StrUitl;
